import PlayEnity from "./enity/Player.js";
import EnemyMaker from "./enity/EnemyMaker.js";

let gameCanvas;
//变量声明
let timer;//计时器
export const GameInfo = {
    gameid:1,
    gamename:'飞机大战'
}
export let context = {
    mouseX: 0,
    mouseY: 0,
    NodeList: [],//实体队列
    time: 0,//游戏时间
    score: 0//分数
};//全局的上下文对象
export function setContext(contextPx){
    context = contextPx
}//为了实现Vue的响应式提供的接口
let ctx;//画笔
export function setGameCanvas(gameCanvasDom) {
    gameCanvas = gameCanvasDom
    //监听鼠标的坐标
    gameCanvas.addEventListener('mousemove', e => {
        context.mouseX = e.offsetX
        context.mouseY = e.offsetY
        if(window.innerWidth<400){
            context.mouseX *= 400/window.innerWidth
            context.mouseY *= 400/window.innerWidth
        }
    })
    gameCanvas.addEventListener('touchstart', e => {
        e.preventDefault();
        context.mouseX = e.touches[0].pageX
        context.mouseY = e.touches[0].pageY- gameCanvas.parentNode.offsetTop
        console.log(context.mouseY)
        if(window.innerWidth<400){
            context.mouseX *= 400/window.innerWidth
            context.mouseY *= 400/window.innerWidth
        }
    })
    gameCanvas.addEventListener('touchmove',e => {
        e.preventDefault();
        e.stopPropagation();
        context.mouseX = e.touches[0].clientX
        context.mouseY = e.touches[0].pageY- gameCanvas.parentNode.offsetTop
        if(window.innerWidth<400){
            context.mouseX *= 400/window.innerWidth
            context.mouseY *= 400/window.innerWidth
        }
    })
    ctx = gameCanvas.getContext('2d');
}
//配置
let backgroudColor = 'rgb(0,0,0)'
//面板绘制
function draw(ctx) {
    ctx.beginPath()
    ctx.fillText("分数：" + context.score, 50, 10)
    ctx.fill()
    ctx.closePath()
}
//初始化游戏
export function initGame() {
    context.time = 0
    context.NodeList = []
    context.NodeList = [new EnemyMaker(1)]
    context.gameOver = false
    /*
    玩家为特殊对象，玩家被要求放置到固定的位置，该数组的第二位。
    （为了方便其他节点无需遍历直接通过下下标获取玩家对象的内部属性）
    */
    context.NodeList.push(new PlayEnity(200, 200));

    // context.NodeList.push(new FireProps(200,10))
    context.score = 0
    ctx.fillStyle = 'white'
    ctx.font = '50px 宋体'
    ctx.save()
}

//开始游戏
export function startGame() {
    if (timer) return console.log('定时器存在！不进行处理')
    timer = setInterval(function () {
        //计时
        context.time += 20 * 0.001
        //清除画布
        ctx.fillStyle = backgroudColor
        ctx.strokeStyle = backgroudColor
        ctx.fillRect(0, 0, 400, 400)
        //面板绘制
        draw(ctx)
        //实体操作
        for (let i = 0; i < context.NodeList.length; i++) {
            let date = new Date()
            context.NodeList[i].draw(ctx)//绘制实体
            context.NodeList[i].behavior(context)//实体的行为
            context.NodeList[i].isOK(context);//实体状态判断
            if (!context.NodeList[i].OK) {//实体回收
                context.NodeList[i] = null
                context.NodeList.splice(i, 1)
            }     
        }
    }, 20)
}
//暂停游戏
export function pauseGame() {
    clearInterval(timer)
    timer = null
}
//结束游戏
export function endGame() {
    pauseGame()
    initGame()
}